﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Suppliers : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_supplier_master model = new tbl_supplier_master();
            model.USER_CODE = txtUSER_CODE.Text.Trim();
            model.SUPPLIER_NAME = txtSUPPLIER_NAME.Text.Trim();
            model.SUPPLIER_ADDRESS = txtSUPPLIER_ADDRESS.Text.Trim();
            model.SUPPLIER_CITY = txtSUPPLIER_CITY.Text.Trim();
            model.SUPPLIER_STATE = txtSUPPLIER_STATE.Text.Trim();
            model.SUPPLIER_PINCODE = txtSUPPLIER_PINCODE.Text.Trim();
            model.SUPPLIER_PRIMARY_CONTACT_NO = txtSUPPLIER_PRIMARY_CONTACT_NO.Text.Trim();
            model.SUPPLIER_SECONDARY_CONTACT_NUMBER = txtSUPPLIER_SECONDARY_CONTACT_NUMBER.Text.Trim();
            model.SUPPLIER_EMAIL_ID = txtSUPPLIER_EMAIL_ID.Text.Trim();
            model.SUPPLIER_SKYPE_ID = txtSUPPLIER_SKYPE_ID.Text.Trim();

            model.Payment_Information = txtPayment_Information.Text.Trim();
            model.Bank_Account = txtBank_Account.Text.Trim();
            if (fuAgreement_Document.HasFile)
                model.Supplier_Agreement_Original_Name = fuAgreement_Document.PostedFile.FileName;
            else
                model.Supplier_Agreement_Original_Name = "";

            model.Supplier_Agreement = uploadFiles("Agreement");

            model.Fax = txtFax.Text.Trim();

            model.LinkedIn_ID = txtLinkedIn_ID.Text.Trim();
            model.Tax_Vat_ID = txtTax_Vat_ID.Text.Trim();

            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_supplier_master bll = new BLL_tbl_supplier_master();
            try
            {



                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_supplier_master model = new tbl_supplier_master();
            model.USER_CODE = btnSave.CommandArgument.Trim();
            model.SUPPLIER_NAME = txtSUPPLIER_NAME.Text.Trim();
            model.SUPPLIER_ADDRESS = txtSUPPLIER_ADDRESS.Text.Trim();
            model.SUPPLIER_CITY = txtSUPPLIER_CITY.Text.Trim();
            model.SUPPLIER_STATE = txtSUPPLIER_STATE.Text.Trim();
            model.SUPPLIER_PINCODE = txtSUPPLIER_PINCODE.Text.Trim();
            model.SUPPLIER_PRIMARY_CONTACT_NO = txtSUPPLIER_PRIMARY_CONTACT_NO.Text.Trim();
            model.SUPPLIER_SECONDARY_CONTACT_NUMBER = txtSUPPLIER_SECONDARY_CONTACT_NUMBER.Text.Trim();
            model.SUPPLIER_EMAIL_ID = txtSUPPLIER_EMAIL_ID.Text.Trim();
            model.SUPPLIER_SKYPE_ID = txtSUPPLIER_SKYPE_ID.Text.Trim();
            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);

            model.Payment_Information = txtPayment_Information.Text.Trim();
            model.Bank_Account = txtBank_Account.Text.Trim();
            if (fuAgreement_Document.HasFile)
            {
                model.Supplier_Agreement_Original_Name = fuAgreement_Document.PostedFile.FileName;
                model.Supplier_Agreement = uploadFiles("Agreement");
            }
            else
            {
                model.Supplier_Agreement_Original_Name = txtAgreement_Document.Attributes["OriginalFileName"];
                model.Supplier_Agreement = txtAgreement_Document.Attributes["GuidFileName"];
            }

            model.Fax = txtFax.Text.Trim();
            model.LinkedIn_ID = txtLinkedIn_ID.Text.Trim();
            model.Tax_Vat_ID = txtTax_Vat_ID.Text.Trim();

            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_supplier_master bll = new BLL_tbl_supplier_master();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    private string uploadFiles(string docType)
    {
        string guid = "";
        string guidPath = "";
        bool isFileSaved = false;
        if (docType == "Agreement")
        {
            if (fuAgreement_Document.HasFile)
            {
                guid = Guid.NewGuid().ToString() + Path.GetExtension(fuAgreement_Document.FileName);
                guidPath = Server.MapPath("~/AllFiles/Suppliers/Agreement/" + guid);
                fuAgreement_Document.SaveAs(guidPath);
                isFileSaved = true;
            }
        }
        

        if (!isFileSaved)
            guid = "";

        return guid;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_supplier_master model = new tbl_supplier_master();
        model.USER_CODE = id.ToString();

        BLL_tbl_supplier_master bll = new BLL_tbl_supplier_master();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_supplier_master bll = new BLL_tbl_supplier_master();
                List<tbl_supplier_master> list = bll.select(id);
                foreach (tbl_supplier_master tbl in list)
                {
                    btnSave.CommandArgument = tbl.USER_CODE.ToString();
                    txtUSER_CODE.Text = tbl.USER_CODE;
                    txtSUPPLIER_NAME.Text = tbl.SUPPLIER_NAME;
                    txtSUPPLIER_ADDRESS.Text = tbl.SUPPLIER_ADDRESS;
                    txtSUPPLIER_CITY.Text = tbl.SUPPLIER_CITY;
                    txtSUPPLIER_STATE.Text = tbl.SUPPLIER_STATE;
                    txtSUPPLIER_PINCODE.Text = tbl.SUPPLIER_PINCODE;
                    txtSUPPLIER_PRIMARY_CONTACT_NO.Text = tbl.SUPPLIER_PRIMARY_CONTACT_NO;
                    txtSUPPLIER_SECONDARY_CONTACT_NUMBER.Text = tbl.SUPPLIER_SECONDARY_CONTACT_NUMBER;
                    txtSUPPLIER_EMAIL_ID.Text = tbl.SUPPLIER_EMAIL_ID;
                    txtSUPPLIER_SKYPE_ID.Text = tbl.SUPPLIER_SKYPE_ID;

                    txtPayment_Information.Text = tbl.Payment_Information;
                    txtBank_Account.Text = tbl.Bank_Account;
                    fuAgreement_Document.Style.Add("display", "none");
                    txtAgreement_Document.Style.Remove("display");
                    btnRemoveAgreement.Style.Remove("display");
                    txtAgreement_Document.Attributes["OriginalFileName"] = tbl.Supplier_Agreement_Original_Name;
                    txtAgreement_Document.Attributes["GuidFileName"] = tbl.Supplier_Agreement;

                    string agreementURL = ResolveUrl("~/AllFiles/Clients/Agreement/" + tbl.Supplier_Agreement);
                    txtAgreement_Document.Text = "<a href='" + agreementURL + "'>" + tbl.Supplier_Agreement_Original_Name + "</a>";

                    txtFax.Text = tbl.Fax;
                    txtLinkedIn_ID.Text = tbl.LinkedIn_ID;
                    txtTax_Vat_ID.Text = tbl.Tax_Vat_ID;

                    chkIS_ACTIVE.Checked = tbl.IS_ACTIVE;
                }
            }
        }
    }


                
}